package org.vectrics.common.view;

/**
 * @author Mike Moore
 * @version 1.0
 */
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.Row;
import org.apache.ecs.html.A;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import org.apache.log4j.*;

public class MenuBuilder {
    private static Category log = Category.getInstance(MenuBuilder.class);
    private Menu menu = null;
    private String graphicsDirectory = null;

    public MenuBuilder() {
    }

    public void setGraphicsDirectory(String dir) {
        this.graphicsDirectory = dir;
    }

    public String getGraphicsDirectory() {
        return(this.graphicsDirectory);
    }

    // This is simple rendering.  Should override this to make different menu appearance.
    protected void renderItem(MenuItem item, Row menuRow, boolean selected) {
        Cell itemCell = menuRow.addCell();
        A link = new A();
        if (itemLinkClass != null)
            link.setClass(itemLinkClass);
        link.setHref(item.getLink());
        if (selected)
            link.addElement("<b>");
        link.addElement(item.getDescription());
        if (selected)
            link.addElement("</b>");
        itemCell.addElement(link);
    }

    protected void onBeforeItems(Row menuRow) {
    }

    protected void onAfterItems(Row menuRow) {
    }

    /*
    public boolean isItemSelected(HttpServletRequest request, MenuItem item) {
        if (menu.getSelectItem() == item)
           return(true);
        else
            return(false);
    }
    */
    public void setMenu(Menu menu) {
        this.menu = menu;
    }

    public Menu getMenu() {
        return(menu);
    }

    protected String getMenuCellClass() {
        return("");
    }



    public void build(HttpServletRequest request, Cell menuCell, Menu menu) {
        if (menu == null) {
            log.error("Error, MenuBuilder was never passed a NULL menu item.");
        }
        else {
            setMenu(menu);
            Table mainTable = new Table();
            menuCell.addElement(mainTable);
            menuCell.setClass(getMenuCellClass());
            //mainTable.setWidth("100%");
            mainTable.setCellPadding(0);
            mainTable.setCellSpacing(0);
            Row mainRow = mainTable.addRow();
            onBeforeItems(mainRow);
            Iterator iter = menu.getChildList().iterator();
            while (iter.hasNext()) {
                MenuItem item = (MenuItem)iter.next();
                //boolean itemSelected = isItemSelected(request, item);
                //renderItem(item, mainRow, itemSelected);
            }
            onAfterItems(mainRow);

        }
    }
    public void setItemLinkClass(String itemLinkClass) {
        this.itemLinkClass = itemLinkClass;
    }
    public String getItemLinkClass() {
        return itemLinkClass;
    }
    private String itemLinkClass;
}

/*
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr class="mainmenu">
            <td class="mainMenuBg"><img border="0" src="../graphics/m_blank.gif" width="10" height="24"></td>
            <td class="mainMenuBg"><img border="0" src="../graphics/menu_div.gif" width="2" height="24"></td>
            <td class="mainMenuBg"><a class="lnkMainmenu" href="/weblog/main/admin?action=dash"><img border="0" src="../graphics/m_dash.gif" ></a></td>
            <td class="mainMenuBg"><img border="0" src="../graphics/menu_div.gif" width="2" height="24"></td>
            <td class="mainMenuBg"><img border="0" src="../graphics/m_blank.gif" width="10" height="24"></td>
            <td class="mainMenuBg"><a class="lnkMainmenu" href="/weblog/main/admin?action=cfgmgr"><img border="0" src="../graphics/m_config.gif" ></a></td>
            <td class="mainMenuBg"><img border="0" src="../graphics/m_blank.gif" width="10" height="24"></td>
            <td class="mainMenuBg"><img border="0" src="../graphics/menu_div.gif" width="2" height="24"></td>
            <td class="mainMenuBg"><a class="lnkMainmenu" href="/weblog/main/admin?action=current_log"><img border="0" src="../graphics/m_cur_log.gif" ></a></td>
            <td class="mainMenuBg"><img border="0" src="../graphics/menu_div.gif" width="2" height="24"></td>
            <td class="mainMenuBg"><a class="lnkMainmenu" href="/weblog/main/admin?action=log_search"><img border="0" src="../graphics/ms_search.gif" ></a></td>
            <td class="mainMenuBg"><img border="0" src="../graphics/menu_div.gif" width="2" height="24"></td>
            <td class="mainMenuBg"><a class="lnkMainmenu" href="/weblog/main/admin?action=lvlmgr"><img border="0" src="../graphics/m_lvlmgr.gif" ></a></td>
            <td class="mainMenuBg"><img border="0" src="../graphics/menu_div.gif" width="2" height="24"></td>
            <td class="mainMenuBg"><a class="lnkMainmenu" href="/weblog/main/admin?action=help"><img border="0" src="../graphics/m_help.gif" ></a></td>
            <td class="mainMenuBg"><img border="0" src="../graphics/menu_div.gif" width="2" height="24"></td>
            <td class="mainMenuBg" width="1000"><img border="0" src="../graphics/m_blank.gif" width="100%" height="24"></td>
          </tr>
        </table>
*/
